package top.yokey.learn2.view;

import android.Manifest;
import android.content.ContentQueryMap;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;

import java.util.List;

import top.yokey.learn2.R;
import top.yokey.learn2.ToastUtil;

public class ContactsActivity extends AppCompatActivity {

    private ContactsActivity  contactsSQLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private AppCompatTextView allTextView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
    }

    //读取系统通讯录
    private void readContacts() {

        Cursor cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                null,
                null,
                null
        );
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                    .replace(" ", "");
            Log.d("TAGS", name + "|" + number);
        }
        cursor.close();

    }

    //读取SMS短信
    private void readSms() {

        Uri uri = Uri.parse("content://sms/");
        String[] strings = new String[]{"_id", "address", "person", "body", "date", "type"};
        Cursor cursor = getContentResolver().query(uri, strings, null, null, "date desc");
        while (cursor.moveToNext()) {
            int _id = cursor.getInt(cursor.getColumnIndex("_id"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            String person = cursor.getString(cursor.getColumnIndex("person"));
            String body = cursor.getString(cursor.getColumnIndex("body"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            String typeText = "接收";
            if (type == 0) typeText = "所有";
            if (type == 1) typeText = "接收";
            if (type == 2) typeText = "发送";
            if (type == 3) typeText = "草稿箱";
            if (type == 4) typeText = "发件箱";
            if (type == 5) typeText = "失败";
            if (type == 6) typeText = "待发送";
            Log.d("TAGS", address + "|" + body + "|" + typeText);
        }

    }

    //读取通话记录
    private void readCallLog() {
        String[] strings = new String[]{
                CallLog.Calls.CACHED_NAME, //名字
                CallLog.Calls.NUMBER, //号码
                CallLog.Calls.DATE, //时间
                CallLog.Calls.DURATION,//通话了多少秒
                CallLog.Calls.TYPE//通话类型
        };
        Cursor cursor = getContentResolver().query(
                CallLog.Calls.CONTENT_URI, strings, null, null, null);
        while (cursor.moveToNext()) {
            //String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            String date = cursor.getString(2);
            String duration = cursor.getString(3);
            int type = cursor.getInt(4);
            String typeText = "未知的";
            switch (type) {
                case CallLog.Calls.INCOMING_TYPE:
                    typeText = "来电";
                    break;
                case CallLog.Calls.OUTGOING_TYPE:
                    typeText = "去电";
                    break;
                case CallLog.Calls.MISSED_TYPE:
                    typeText = "未接";
                    break;
            }
            Log.d("TAGS", name + "|" + number + "|" + date + "|" + duration + "|" + typeText);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        XXPermissions.with(this).permission(getPermissions1()).request(new OnPermissionCallback() {
            @Override
            public void onGranted(List<String> permissions, boolean all) {
                //这是所有权限都通过了授权
                readContacts();
                readSms();
                readCallLog();
            }

            @Override
            public void onDenied(List<String> permissions, boolean never) {
                //权限被拒绝或者部分权限未完整授权
                Log.d("TAGS", "123");
            }
        });
    }

    private String[] getPermissions1() {

        return new String[]{
                Manifest.permission.READ_SMS,//读短信
                Manifest.permission.READ_CALL_LOG,//读通话记录
                Manifest.permission.READ_CONTACTS,//读联系人信息
        };

    }

}


        //ContactsSQLiteOpenHelper helper = new ContactsSQLiteOpenHelper(this,"contacts",null,1);
        //SQLiteDatabase database = helper.getWritableDatabase();

       // String sq1 = "insert into contacts(name,phone) values(?,?)";
            //database.execSQL(sq1, new String[]{"张三","10086"});
        //database.execSQL(sq1, new String[]{"最新版","10010"});

        //findViewById(R.id.mineTextView).setOnClickListener(view -> {
          //  Cursor cursor = database.rawQuery("select * from contacts", null);


